//    Copyright (C) Kherty.  All rights reserved.
using System.Collections.Generic;
using System.Diagnostics;
using OpenLS.Core.Serialization;

namespace OpenLS.Chart
{
    /// <summary>
    /// Represents a 3D area chart
    /// </summary>
    public class Area3DChart : AreaChartBase, I3DChart
    {
        public Area3DChart()
        {
            FirstAxis = new ValueAxis { Position = AxisPosition.Left };
            SecondAxis = new CategoryAxis();
            ThirdAxis = new SeriesAxis();
            FirstAxis.CrossAxis = SecondAxis;
            SecondAxis.CrossAxis = FirstAxis;
            ThirdAxis.CrossAxis = SecondAxis;
        }
        /// <summary>
        /// Get the first vertical axis
        /// </summary>
        public Axis VerticalAxis
        {
            get
            {
                return Enumerable.FirstOrDefault(GetAxes(),
                                                 a =>
                                                 a.Position == AxisPosition.Left || a.Position == AxisPosition.Right);
            }
        }

        private Axis _firstAxis;
        private int _gapDepth;
        private Axis _secondAxis;
        private Axis _thirdAxis;

        ///<summary>
        /// Get or set the depth of the gap between series
        ///</summary>
        public int GapDepth
        {
            get { return _gapDepth; }
            set { _gapDepth = value; notifyPropertyChanged("GapDepth"); }
        }

        public Axis FirstAxis
        {
            get { return _firstAxis; }
            set
            {
                OnAxisChanged(_firstAxis, value);
                _firstAxis = value;
            }
        }

        public Axis SecondAxis
        {
            get { return _secondAxis; }
            set
            {
                OnAxisChanged(_secondAxis, value);
                _secondAxis = value;
            }
        }

        public Axis ThirdAxis
        {
            get { return _thirdAxis; }
            set
            {
                OnAxisChanged(ThirdAxis, value);
                _thirdAxis = value;
            }
        }

        public ValueAxis ValueAxis
        {
            get
            {
                foreach (var axis in GetAxes())
                {
                    if (axis is ValueAxis)
                        return (ValueAxis)axis;
                }
                return null;
            }
        }
        public CategoryAxis CategoryAxis
        {
            get
            {
                foreach (Axis axis in GetAxes())
                {
                    if (axis is CategoryAxis)
                        return (CategoryAxis)axis;
                }
                return null;
            }
        }

        protected override void VerifyAxes()
        {
            Debug.WriteLine("todo //\\" + GetType());
        }

        internal override void ReadXmlCore(ReadContext context)
        {
            using (var c = context.Read("area3DChart"))
            {
                ReadXmlBase(c);
                _gapDepth = c.ReadOptionalElementIntegerAttribute("gapDepth", 150);
                AxisResolver r = ReadContextData.GetAxisResolver(context);
                r.AddAction(c.ReadElementUIntegerAttribute("axId"),
                            delegate(Axis a) { FirstAxis = a; });
                r.AddAction(c.ReadElementUIntegerAttribute("axId"),
                            delegate(Axis a) { SecondAxis = a; });
                r.AddAction(c.ReadOptionalElementUIntegerAttribute("axId"),
                            delegate(Axis a) { ThirdAxis = a; });
                Extension = c.ReadOptionalElement<ExtensionElement>("extLst");
            }
        }

        internal override void WriteXmlCore(WriteContext context)
        {
            using (WriteContext c = context.Write("area3DChart"))
            {
                WriteXmlBase(c);
                c.WriteOptionalElementIntegerAttribute("gapDepth", _gapDepth, 150);
                c.WriteElementUIntegerAttribute("axId", FirstAxis.Id);
                c.WriteElementUIntegerAttribute("axId", SecondAxis.Id);
                if (ThirdAxis != null)
                    c.WriteElementUIntegerAttribute("axId", ThirdAxis.Id);
                c.WriteOptionalElement(Extension);
            }
        }

        internal override IEnumerable<Axis> GetAxes()
        {
            yield return FirstAxis;
            yield return SecondAxis;
            yield return ThirdAxis;
        }
    }
}